# -*- coding: utf-8 -*-
"""
    201901, Dr. Jie Zheng, Beijing & Xinglong, NAOC
    202101-? Dr. Jie Zheng & Dr./Prof. Linqiao Jiang
    Light_Curve_Pipeline
    v3 (2021A) Upgrade from former version, remove unused code
"""


import numpy as np
from scipy import stats as st


def meanclip(dat, nsigma=3.0, func=np.median):
    """
    Compute clipped median and sigma of dat
    :param dat: data, can be list, tuple or np array, 1-d or n-d,
                but if use checking, n-d array will be flattened
    :param nsigma: how many sigma used in clipping
    :param func: method used to evaluate the median/mean
    :return:
    """
    if len(dat) == 0:
        m, s = np.nan, np.nan
        ix = []
    elif len(dat) == 1:
        m, s = dat[0], np.nan
        ix = [0]
    else:
        dat1 = dat.flatten()
        dat1 = dat1[np.isfinite(dat1)]
        c, l, u = st.sigmaclip(dat1, nsigma, nsigma)
        m = func(c)
        s = np.std(c)
    return m, s


def sex2dec(s, fac=1.0):
    """
    将六十进制表达的数值转换成十进制表达（hms不负责乘15）
    :param s: 字符串形式，可以是空格分隔或者冒号分隔，如果不是字符串不转换，包括None
    :param fac: 倍率，如果是字符串转浮点数，需要乘倍率
    :return:
    """
    if type(s) not in (str, np.str_):
        d = s
    else:
        s1 = s.replace(":", " ").split()
        d = float(s1[0]) + float(s1[1]) / 60.0 + float(s1[2]) / 3600.0
        d *= fac
    return d




